//
//  RNiPhonePlayViewController.m
//  RecallNote
//
//  Created by Z on 14/12/13.
//  Copyright (c) 2014年 Project. All rights reserved.
//

#import "RNiPhonePlayViewController.h"
#import "RNCommonBlockView.h"
#import "RNMusicLibrary.h"
#import "RNRadioButton.h"
#import <AVFoundation/AVFoundation.h>

#define GAME_SCENE_ORGIN_Y  92

@interface RNiPhonePlayViewController ()<RNCommonBlockViewDelegate,UITextFieldDelegate>

@property (nonatomic , strong) NSMutableArray   *blockViews;
@property (nonatomic , strong) NSTimer          *playTimer;
@property (nonatomic , assign) NSInteger        currentPoint;
@property (nonatomic , assign) NSInteger        currentRadioCount;
@property (nonatomic , strong) NSArray          *currentNotes;
@property (nonatomic , strong) NSMutableArray   *radioButtons;

@property (nonatomic , weak) IBOutlet UITextField *textfield;

@end

@implementation RNiPhonePlayViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self prepareCommonViews];
    
    [self prepareWithCount:5];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];

}

- (IBAction)autoPlay:(id)sender
{
    _currentNotes = [[[[RNMusicLibrary alloc] noteLibrary] componentsSeparatedByString:@","] copy];
//    _currentNotes = [[RNMusicLibrary alloc] notesWithCount:[self.textfield.text integerValue]];
    _playTimer = [NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(handlerAutoPlayTimer:) userInfo:nil repeats:YES];
}
- (IBAction)replay:(id)sender
{
    _playTimer = [NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(handlerAutoPlayTimer:) userInfo:nil repeats:YES];
}

- (void)handlerAutoPlayTimer:(NSTimer *)timer
{
    if (![_currentNotes[self.currentPoint] isEqualToString:@" "])
    {
//        NSInteger tag = [note integerValue];
//        RNCommonBlockView *blockView = self.blockViews[tag-1];
//        [blockView handleTapGesture:nil];
        
        NSString *note = [NSString stringWithFormat:@"sound_%@",_currentNotes[self.currentPoint]];
        NSLog(@"%@",note);
        [self playAudio:note];
    }
    
    _currentPoint ++;
    if (_currentPoint == _currentNotes.count)
    {
        [timer invalidate];
        timer = nil;
        _currentPoint = 0;
    }
}

- (void) prepareCommonViews
{
    _blockViews = [@[] mutableCopy];
    NSArray *colors = [ColorHex componentsSeparatedByString:@","];
    for(NSInteger i=0; i<15; i++)
    {
        if (i==7)
        {
            continue;
        }
        NSString *soundName = @"";
        if (i <= 7)
        {
            soundName = [NSString stringWithFormat:@"sound_%ld",i+1];
        }
        else
        {
            soundName = [NSString stringWithFormat:@"sound_%ld",i];
        }
        
        RNCommonBlockView *blockView = [RNCommonBlockView viewFromXib];
        blockView.delegate = self;
        blockView.backgroundColor = [UIColor colorWithHexString:colors[i]];
        blockView.soundName = soundName;
        blockView.soundTag = i+1;
        [blockView renderView];
        blockView.origin = CGPointMake(1.5+i%3*(blockView.sizeW + 1), GAME_SCENE_ORGIN_Y+i/3*(blockView.sizeH +1));
        [self.view addSubview:blockView];
        [_blockViews addObject:blockView];
    }
}

- (void)commonBlockDidSelectAtTag:(NSInteger)tag
{
    if(self.currentRadioCount < self.radioButtons.count)
    {
        RNRadioButton *button = self.radioButtons[self.currentRadioCount];
        button.type = Radio_Right;
        [button updateState];
        
        self.currentRadioCount ++;
    }
}


- (void)prepareWithCount:(NSInteger) count
{
    self.radioButtons = [@[] mutableCopy];
    for(NSInteger i=0; i<count ;i++)
    {
        RNRadioButton *button = [RNRadioButton viewFromXib];
        button.originX = 80 + i*25;
        button.originY = 50;
        [button render];
        [self.topViewContainer addSubview:button];
        [self.radioButtons addObject:button];
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

-(void)playAudio:(NSString*)audioName
{
    static SystemSoundID shake_sound_male_id = 0;
    
    NSString *path = [[NSBundle mainBundle] pathForResource:audioName ofType:@"mp3"];
    if (path) {
        //注册声音到系统
        AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path],&shake_sound_male_id);
        AudioServicesPlaySystemSound(shake_sound_male_id);
    }
    
    AudioServicesPlaySystemSound(shake_sound_male_id);
}

@end
